package hk.cic.demointegrationapp;

import android.os.StrictMode;

import java.io.*;

import java.io.InputStreamReader;
import java.net.*;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static java.lang.System.in;

/**
 * Created by tim on 2017/8/19.
 */

public class HttpPost {
    public static String HttpRequestAPI(String pathUri,String type, String json) {
        String lines = null;
        try {
            //创建连接
            URL url = new URL(pathUri);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod(type);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Charset", "UTF-8");

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            connection.connect();
            //POST请求
            OutputStream  out = connection.getOutputStream();
            // 写入请求的字符串
            out.write(json.getBytes());
            out.flush();
            out.close();

            if(connection.getResponseCode() == 200) {
                //BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                InputStream in = connection.getInputStream();
                try {
                    byte[] data1 = new byte[in.available()];
                    in.read(data1);
                    // 转成字符串
                    lines = new String(data1);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            // 断开连接
            connection.disconnect();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return lines;
    }

    public static final MediaType JSON  = MediaType.parse("application/json; charset=utf-8");

    OkHttpClient client = new OkHttpClient();

    public  String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        if(!response.isSuccessful()) {
            throw new IOException("服务器端错误: " + response);
        }
        String result =  response.body().string();
        return response.body().string();
    }
}
